<?php
// +----------------------------------------------------------------------
// | 后台用户管理
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
use base\pay\WxApi;
use base\other\Strings;
use app\common\model\User;
use app\common\model\UserBill;
use app\common\model\UserRechargeRule;

class UserRecharge extends Model
{
	use \base\model\ModelTrait;
	
	// 自动完成
	protected $insert = ['no','type'];
	protected function setNoAttr() {
		return Strings::createOrderNo('cz');
    }
    protected function setTypeAttr($value) {
		return $value ?: 'wx';
    }
	/**
    * 验证规则
    */
	protected $rule = [
		'uid|用户id'=>'require|integer',
		'price|充值金额'=>'require|float|min:0.1',
	];
	
	
	/**
	* 获取最新的一条充值状态
	*/
	public function getRechargeStatus($uid){
		$order = $this->where('uid',$uid)->limit(1)->order("add_time desc")->find();
		$payInfo =  (new WxApi)->orderQuery($order->no,true);
		if($order->status == 0 && $payInfo['trade_state_desc']=="支付成功"){
			$order->status = 1;
			$order->status_time = date("Y-m-d H:i:s");
			$order->transaction_id = $payInfo['transaction_id'];
			$order->save();
			return event('VipRechargeSuccess',$order);
		}
		return $order;
	}
	
	/**
	* 充值成功
	*/
	public function paySuccess($no,$transaction_id='',$is_admin=false){
		$order = self::where('no',$no)->find();
		if($order->status == 1){
			return true;
		}else{
			//添加到账户
	    	$uid = $order['uid'];
	    	$price = floatval($order['price']);
	    	
	    	$user = User::get($uid);
	    	
	    	//获取赠送规则
	    	$giveBalance = 0;
	    	$giveIntegral= 0;
	    	if($is_admin==false){
	    		$rule = UserRechargeRule::where('status',0)
	    			->where("shop_id", $user->shop_id )
		    		->where('price','<=',$price)
		    		->where(function ($query) {
			    		$query->where(function ($query)  {
							$query->whereNull('expire_time');
						});
						$query->whereor(function ($query)  {
							$query->whereNotNull('expire_time');
							$query->where([['','exp',\Db::raw("date_format(now(),'%Y-%m-%d %H:%i:%s') < expire_time")]]);
						});
					})->order('price desc')->limit(1)->findOrEmpty();
		    	
		    	//获取赠送项
		    	$giveBalance = $rule['balance_gift'] ?? 0;
		    	$giveIntegral= $rule['integral_gift'] ?? 0;
	    	}
	    	
	    	//入账
	    	$user->balance = ['inc', $price];
	    	$user->balance_gift = ['inc', $giveBalance];
	    	$user->integral = ['inc', $giveIntegral];
	    	$user->save();
	    	
	    	//添加流水账
	    	$mark = '会员充值入账'.$price.'元';
	    	UserBill::income("会员充值",$uid,'balance','recharge',$price,$order['no'],0,$mark);
	    	if($giveBalance){
	    		$mark= '会员充值赠送'.$giveBalance.'余额';
	    		UserBill::income("会员充值",$uid,'balance_gift','gain',$giveBalance,$order['no'],0,$mark);
	    	}
	    	if($giveIntegral){
	    		$mark= '会员充值赠送'.$giveIntegral.'积分';
	    		UserBill::income("会员充值",$uid,'integral','gain',$giveIntegral,$order['no'],0,$mark);
	    	}
	    	
	    	//更改订单状态
	    	$order->status = 1;
	    	$order->give_integral = $giveIntegral;
	    	$order->give_balance = $giveBalance;
			$order->status_time = date("Y-m-d H:i:s");
			$order->transaction_id = $transaction_id;
			$order->save();
		}
	}
	
		
}
